package com.guli.gulimall.order.config;

import com.guli.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *容器中的 Binding Queue Exchange 都会自动创建（前提是RabbitMq中没有的情况）
 * 一旦队创建成功，后续修改代码，不会覆盖队列，只能手动删除队列，重新运行代码
 */
@Configuration
public class MyMQConfig {


    /**
     * 交换机
     *
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        /**
         * TopicExchange(String name, boolean durable, boolean autoDelete, Map<String, Object> arguments)
         *  name：交换机名称
         *  durable:是否持久化
         *  autoDelete:是否自动删除
         *  arguments:参数
         */
        TopicExchange exchange = new TopicExchange("order-event-exchange", true, false);
        return exchange;
    }

    /**
     * 死信队列（没有订阅方，超时自动放入其他队列，方便使用（延迟队列实现））
     *
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {
        /**
         Queue(
         String name,  队列名字
         boolean durable,  是否持久化
         boolean exclusive,  是否排他
         boolean autoDelete, 是否自动删除
         Map<String, Object> arguments 属性
         )
         */
        Map<String, Object> arguments = new HashMap<>();
        //死信交换机
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        //死信路由键
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 60000); // 消息过期时间 1分钟

        Queue queue = new Queue("order.delay.queue", true, false, false, arguments);
        return queue;
    }

    /**
     * c端队列，死信队列超时后的接收队列（与c端交互）
     *
     * @return
     */
    @Bean
    public Queue orderReleaseOrderQueue() {
        Queue queue = new Queue("order.release.order.queue", true, false, false);
        return queue;
    }


    /**
     * 死信队列和交换机绑定
     * @return
     */
    @Bean
    public Binding orderCreateOrderBinding() {
        /**
         * Binding(String destination, Binding.DestinationType destinationType, String exchange, String routingKey, @Nullable Map<String, Object> arguments)
         * destination：目的地
         * destinationType：目的地类型
         * exchange：交换机
         * routingKey：路由键
         * arguments：指定参数
         *
         * 将指定的交换机和目的地进行绑定，指定目的地类型
         *
         */
        Binding binding = new Binding("order.delay.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.create.order", null);
        return binding;
    }

    /**
     * c端交互队列和交换机绑定
     * @return
     */
    @Bean
    public Binding orderReleaseBinding() {
        Binding binding = new Binding("order.release.order.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.order", null);
        return binding;
    }


    /**
     * 订单释放直接和库存释放进行绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding() {
        Binding binding = new Binding(
                "stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
        return binding;
    }

    /**
     * 秒杀队列
     * @return
     */
    @Bean
    public Queue orderSeckillOrderQueue(){
        Queue queue = new Queue("order.seckill.order.queue", true, false, false);
        return queue;
    }

    /**
     * 秒杀队列和交换机绑定
     * @return
     */
    @Bean
    public Binding orderSeckillOrderBinding() {
        Binding binding = new Binding(
                "order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
        return binding;
    }

}
